package com.example.meijun.myapplication;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by meijun on 17-3-28.
 */

public class SweepView  extends View{
    private static final int DEFAULT_WIDTH = 100;
    private static final int DEFAULT_HEIGHT = 100;
    private int mWidth;
    private int mHeight;
    private RectF rectF;
    private Paint paint;
    private int mColor = Color.RED;

    /**
     * 设置扇形颜色
     * UIThred
     * @param mColor
     */
    public void setColor(int color) {
        this.mColor = color;
        paint.setColor(mColor);
        invalidate();
    }

    /**
     * 设置扇形的区域0-360
     *
     * UIThread
     *
     * @param mSweep
     */
    public void setSweep(float mSweep) {
        this.mSweep = mSweep;
        invalidate();
    }

    private float mSweep = 130;

    public SweepView(Context context) {
        super(context);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setColor(mColor);
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);

    }

    public SweepView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SweepView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int resultWidth = measureWidth(widthMeasureSpec);

        int resultHeight = measureHeight(heightMeasureSpec);
       setMeasuredDimension(resultWidth,resultHeight);
    }

    private int measureWidth(int widthMeasureSpec) {
        int size = MeasureSpec.getSize(widthMeasureSpec);
        int mode = MeasureSpec.getMode(widthMeasureSpec);
        int result  ;
        if(mode==MeasureSpec.EXACTLY){
             result = size;
        }else{
            result = DEFAULT_WIDTH;
            if(mode==MeasureSpec.AT_MOST){
                result= Math.min(size,DEFAULT_WIDTH);
            }

        }
    return result;
    }

    private int measureHeight(int heightMeasureSpec) {

        int size = MeasureSpec.getSize(heightMeasureSpec);
        int mode = MeasureSpec.getMode(heightMeasureSpec);
        int result  ;
        if(mode==MeasureSpec.EXACTLY){
            result = size;
        }else{
            result = DEFAULT_HEIGHT;
            if(mode==MeasureSpec.AT_MOST){
                result= Math.min(size,DEFAULT_HEIGHT);
            }

        }
        return result;
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {

        this.mHeight = h;
        this.mWidth = w;

        rectF = new RectF(0, 0, w, h);
        super.onSizeChanged(w, h, oldw, oldh);



    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawArc(rectF,-90,mSweep,true,paint);

    }
}
